#!/usr/bin/env python
# -*- coding:utf-8 -*-

bl_info = {
    "name": "Simple Add-on Template",
    "author": "姚彧",
    "location": "View3D > Tools > 简单插件",
    "version": (0, 0, 1),
    "blender": (2, 7, 8),
    "description": "开始一个新插件",
    "wiki_url": "http://example.com",
    "category": "Development",
}

import bpy

class SimpleOperator(bpy.types.Operator):
    bl_idname = "object.simple_operator"
    bl_label = "输出鼓励信息插件"

    def execute(self, context):
        print('\n\n####################################################')
        print('# Add-on and Simple Operator executed successfully!')
        print('# ' + context.scene.encouraging_message)
        print('####################################################')
        return {'FINISHED'}

    @classmethod
    def register(cls):
        print('Registered class: %s' % cls.bl_label)

        bpy.types.Scene.encouraging_message = bpy.props.StringProperty(
            name = '',
            description = '输出给用户的信息',
            default = 'Have a nice day!'
        )

    @classmethod
    def unregister(cls):
        print('Unregistered class: %s' % cls.bl_label)
        del bpy.types.Scene.encouraging_message

class SimplePanel(bpy.types.Panel):
    bl_space_type = 'VIEW_3D'
    bl_region_type = 'TOOLS'
    bl_category = '简单插件'
    bl_label = '调用简单操作'
    bl_context = 'objectmode'

    def draw(self, context):
        self.layout.operator('object.simple_operator',
                             text = '鼓 励')
        self.layout.prop(context.scene, 'encouraging_message')

    @classmethod
    def register(cls):
        print("Registered class: %s " % cls.bl_label)
        # Register properties related to the class here.

    @classmethod
    def unregister(cls):
        print("Unregistered class: %s " % cls.bl_label)
        # Delete parameters related to the class here

def register():
    # Implicitly register objects inheriting bpy.types in current file and scope
    #bpy.utils.register_module(__name__)
    # Or explicitly register objects
    bpy.utils.register_class(SimpleOperator)
    bpy.utils.register_class(SimplePanel)
    print("%s 注册完成\n" % bl_info.get('name'))
    pass

def unregister():
    # Always unregister in reverse order to prevent error due to
    # interdependencies
    # Explicitly unregister objects
    # bpy.utils.unregister_class(SimpleOperator)
    # bpy.utils.unregister_class(SimplePanel)
    # Or unregister objects inheriting bpy.types in current file and scope
    bpy.utils.unregister_module(__name__)
    print("%s 注销完成\n" % bl_info.get('name'))
    pass

if __name__ == "__main__":
    try:
        unregister()
    except Exception as e:
        # Catch failure to unregister explicitly
        print(e)
        pass
    register()